package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.jyy.Receive;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.lz.*;
import com.sdry.model.zc.ZcGeneralQueryEntity;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.cb.ReportFormService;
import com.sdry.utils.ResponseUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 报表模块
 * @ClassName
 * @Description
 * @Author cb
 * @Date 2019年8月26日
 * @Version 1.0
 */
@Controller
@RequestMapping("/reportForm")
public class ReportFormController extends BaseController{


    @Autowired
    private ReportFormService reportFormService;

    @RequestMapping("/geLiPage")
    public String geLiPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        return "bb/geLi";
    }

    @RequestMapping("/fanGongPage")
    public String fanGongPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);

        return "bb/fanGong";
    }

    @RequestMapping("/returnPage")
    public String returnPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        return "bb/return";
    }


    /**
     * 月报表
     * @return
     */
    @RequestMapping(value = "getReportFormList")
    @ResponseBody
    public TableDataInfo getReportFormList(
            String selectTime2, Integer page , Integer pageSize, String ma_id, String date,Long customer_id
    ) throws Exception {



        logger.info("获取月报表列表"+"page="+page+",pageSize="+pageSize+",ma_id="+ma_id+",date="+date);
        String firsTime = reportFormService.getReportFormFirsTime();
        PageHelper.startPage(page,pageSize);
        return reportFormService.getReportFormList(ma_id,date,customer_id,firsTime,selectTime2);
    }

    /**
     * 日报表
     * @return
     */
    @RequestMapping(value = "getDateBalance")
    @ResponseBody
    public TableDataInfo getDateBalance(
            String dateTime2 , Integer page , Integer pageSize, String ma_id, String date,Long customer_id
    ) throws Exception {
        logger.info("获取日报表列表"+"page="+page+",pageSize="+pageSize+",ma_id="+ma_id+",date="+date);
        String firsTime = reportFormService.getReportFormFirsTime();
        PageHelper.startPage(page,pageSize);
        return reportFormService.getDateBalance(ma_id,date,customer_id,firsTime,dateTime2);
    }

    /**
     * 入库报表
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormJinList")
    @ResponseBody
    public TableDataInfo getReportFormJinList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    ) {
        logger.info("获取入库报表列表,page="+page+",pageSize="+pageSize+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
        PageHelper.startPage(page,pageSize);
        List<Receive> list = reportFormService.getReportFormJinList(selectTime,maName,date,customer_id,selectType,selectTime2);
        Integer count = reportFormService.getReportFormJinListTotal(selectTime,maName,date,customer_id,selectType,selectTime2);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(), count);
    }


    /**
     * 出库报表
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormChungList")
    @ResponseBody
    public TableDataInfo getReportFormChungList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    ) {
        logger.info("获取出库报表列表,+page="+page+",pageSize="+pageSize+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
        PageHelper.startPage(page,pageSize);
        List<OutStockOrder> list = reportFormService.getReportFormChungList(maName,date,customer_id,selectTime,selectType,selectTime2);
        Integer total = reportFormService.getReportFormChungListTotal(maName,date,customer_id,selectTime,selectType,selectTime2);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(), total);
    }

    /**
     * 退库报表
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormReTurnList")
    @ResponseBody
    public TableDataInfo getReportFormReTurnList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String dateTime2
    ) {
        logger.info("获取退库报表列表,+page="+page+",pageSize="+pageSize+ ",ma_id="+maName+",date="+date);
        PageHelper.startPage(page,pageSize);
        List<LlmCancellingStocks> list = reportFormService.getReportFormReTurnList(selectTime,maName,date,customer_id,selectType,dateTime2);
        Integer total = reportFormService.getReportFormReTurnListTotal(selectTime,maName,date,customer_id,selectType,dateTime2);
        PageInfo pageInfo = new PageInfo(list,5);
        PageHelper.startPage(page,pageSize);
        return getDataTable(pageInfo.getList(), total);
    }


    /**
     * 不良库报表
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormReGeLIList")
    @ResponseBody
    public TableDataInfo getReportFormReGeLIList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    ) throws Exception {
        logger.info("获取不良库报表列表,+page="+page+",pageSize="+pageSize+ ",ma_id="+maName+",date="+date);
        PageHelper.startPage(page,pageSize);
        List<ZcRejectsWarehouseEntity> list = reportFormService.getReportFormReGeLIList(selectTime,maName,date,customer_id,selectType,selectTime2);
        Integer total = reportFormService.getReportFormReGeLIListTotal(selectTime,maName,date,customer_id,selectType,selectTime2);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(), total);
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "getReceiveDetail/{divID}")
    @ResponseBody
    public JSONObject getReceiveDetail(@PathVariable String divID,String dateTime) {
        List<Receive> list = reportFormService.getReceiveDetail(dateTime);
        JSONObject result = new JSONObject();
        //添加入result
        result.put("code", 0);
        result.put("data", list);
        result.put("count", 100);
        return result;
    }

    @RequestMapping(value = "/getReportInventoryList")
    @ResponseBody
    public void getReportInventoryList(HttpServletResponse response,Integer page , Integer pageSize,Long customer_id,String maName,String date) throws Exception {
        logger.info("获取总库存列表,+page="+page+",pageSize="+pageSize+ ",ma_id="+maName+",date="+date);
        PageHelper.startPage(page,pageSize);
        List<ZcInventoryInfoEntity> list = reportFormService.getReportInventoryList(customer_id,maName,date);
        JSONObject result = new JSONObject();
        PageInfo pageInfo = new PageInfo(list,5);
        result.put("code", 0);
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        ResponseUtil.write(response, result);
    }


    @RequestMapping("/pageTotalStock")
    public String pageTotalStock(Model model,HttpSession session){
        ZcGeneralQueryEntity zcGeneralQueryEntity = new ZcGeneralQueryEntity();
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
        if(staf.getRoleId().equals("20010,")) {
            Long customer_id = staf.getCustomer_id();
            model.addAttribute("customer_id",customer_id);
        }
        return "/bb/totalStock";
    }


    @RequestMapping("/getInventoryInfoList")
    @ResponseBody
    public Map<String,Object> getInventoryInfoList(Integer page , Integer pageSize,String mid) {
        List<Materiel> list =  reportFormService.getInventoryInfoList(mid);
        //PageHelper.startPage(page,pageSize);
        //PageInfo pageInfo = new PageInfo(list,5);
        map.put("code", 0);
        map.put("data", list);
        map.put("count", 100);
        return map;
    }

    /**
     * 获取总库存
     * @param
     * @param
     * @param mid
     * @return
     */
    @RequestMapping("/getStockInventoryInfoDetailedList")
    @ResponseBody
    public Map<String,Object> getStockInventoryInfoList(Integer page , Integer limit,String mBatch ,String mid) {
        List<Materiel> list =  reportFormService.getStockInventoryInfoDetailedList(mid,mBatch,page,limit);
        List<Materiel> total = reportFormService.getStockInventoryInfoDetailedListTotal(mid,mBatch);
        map.put("code", 0);
        map.put("data", list);
        map.put("count", total);
        return map;
    }

    /**
     * 母子表内条码表
     * @param mBatch
     * @param mid
     * @param locationCode
     * @return
     */
    @RequestMapping("/getTiaoMaList")
    @ResponseBody
    public Map<String,Object> getTiaoMaList(
            String mBatch ,
            String mid,
            String locationCode,
            String time,
            String man
    ) {
        List<Materiel> list =  reportFormService.getTiaoMaList(mBatch,mid,locationCode);
        for (Materiel mt : list) {
            mt.setBrevity_num(man);
            mt.setOut_date(time);
        }

        map.put("code", 0);
        map.put("data", list);
        map.put("count", 100);
        return map;
    }



}
